# class Dog:
#     """⼀次模拟⼩狗的简单尝试"""
#
#     def __init__(self, name, age):
#         """初始化属性 name 和 age"""
#         self.name = name
#         self.age = age
#
#     def sit(self):
#         """模拟⼩狗收到命令时坐下"""
#         print(f"{self.name} is now sitting.")
#
#     def roll_over(self):
#         """模拟⼩狗收到命令时打滚"""
#         print(f"{self.name} rolled over!")
#
# my_dog = Dog('Willie', 6)
# print(f"My dog's name is {my_dog.name}.")
# my_dog.sit()

class Car:
    """⼀次模拟汽⻋的简单尝试"""
    species = "Transportation Familiaris"  # 类属性

    def __init__(self, make, model, year):
        """初始化描述汽⻋的属性"""
        self.make = make
        self.model = model
        self.year = year
        self.odometer_reading = 0

    def get_descriptive_name(self):
        """返回格式规范的描述性信息"""
        long_name = f"{self.year} {self.make} {self.model}"
        return long_name.title()

    def read_odometer(self, current_year):
        self.cal_odometer(current_year)
        """打印⼀条指出汽⻋⾏驶⾥程的消息"""
        print(f"This car has {self.odometer_reading} miles on it.")

    # @staticmethod
    # def cal_odometer(self_instance, current_year):
    #     self_instance.odometer_reading = (current_year - self_instance.year) * 10
    #     print(f"This car has {self_instance.odometer_reading} miles on it.")

    def cal_odometer(self, current_year):
        self.odometer_reading = (current_year - self.year) * 10

    @classmethod
    def get_species(cls):  # 类方法
        return cls.species  # 访问类属性

# 实例方法调用
my_new_car = Car('audi', 'a4', 2024)
# print(my_new_car.get_descriptive_name())
# my_new_car.read_odometer(2025)

# 类方法调用
# print(Car.get_species())
# Species = Car.get_species()

# 手动传递实例和参数给静态方法（不推荐）
# Car.cal_odometer(my_new_car, 2025)

# 工厂方法
class FileProcessor:
    # 工厂方法：根据文件类型返回对应的处理器
    @classmethod
    def create_processor(cls, file_type):
        if file_type == "json":
            return JsonProcessor(file_type)
        elif file_type == "csv":
            return CsvProcessor(file_type)
        else:
            raise ValueError("不支持的格式")

class JsonProcessor:
    def __init__(self, name):
        self.name = name

    @classmethod
    def process(cls):
        print("处理 JSON 文件")

    def print_name(self):
        print(f"Current processor is {self.name}")

class CsvProcessor:
    def __init__(self, name):
        self.name = name

    @classmethod
    def process(cls):
        print("处理 CSV 文件")

    def print_name(self):
        print(f"Current processor is {self.name}")

# 调用方通过工厂方法创建对象，无需知道具体类
# processor = FileProcessor.create_processor("csv")
# processor.process()  # 输出: 处理 JSON 文件
# processor.print_name()

# 继承
class Parent:
    def parent_method(self):
        print("执行父类方法")

class Child(Parent):
    def child_method(self):
        print("执行子类方法")
        super().parent_method()

# obj = Child()
# obj.child_method()  # 输出：执行子类方法 → 执行父类方法

# 继承时的多态支持
class Animal:
    @classmethod
    def create(cls, name):
        print(f"I got a pet, named {name}")

class Cat(Animal):
    def __init__(self, name):
        self.name = name

class Dog(Animal):
    def __init__(self, name):
        self.name = name

# Cat.create("Tom")
# Dog.create("Jimmy")

class Car:
    """⼀次模拟汽⻋的简单尝试"""

    def __init__(self, make, model, year):
        """初始化描述汽⻋的属性"""
        self.make = make
        self.model = model
        self.year = year
        self.odometer_reading = 0

    def get_descriptive_name(self):
        """返回格式规范的描述性名称"""
        long_name = f"{self.year} {self.make} {self.model}"
        return long_name.title()

    def read_odometer(self):
        """打印⼀个句⼦，指出汽⻋的⾏驶⾥程"""
        print(f"This car has {self.odometer_reading} miles on it.")

    def update_odometer(self, mileage):
        """将⾥程表读数设置为给定的值"""
        if mileage >= self.odometer_reading:
            self.odometer_reading = mileage
        else:
            print("You can't roll back an odometer!")

    def increment_odometer(self, miles):
        """让⾥程表读数增加给定的量"""
        self.odometer_reading += miles


class ElectricCar(Car):
    """电动汽⻋的独特之处"""

    def __init__(self, make, model, year):
        """初始化⽗类的属性"""
        super().__init__(make, model, year)
        self.battery = Battery()


class Battery:
    """⼀次模拟电动汽⻋电池的简单尝试"""

    def __init__(self, battery_size=40):
        """初始化电池的属性"""
        self.battery_size = battery_size

    def describe_battery(self):
        """打印⼀条描述电池容量的消息"""
        print(f"This car has a {self.battery_size}-kWh battery.")

    def get_range(self):
        """打印⼀条消息，指出电池的续航⾥程"""
        brange = None
        if self.battery_size == 40:
            range = 150
        elif self.battery_size == 65:
            range = 225

        if range is not None:
            print(f"This car can go about {range} miles on a full charge.")
        else:
            print(f"Warning: Battery size {self.battery_size}-kWh is not supported. No range available.")

# my_leaf = ElectricCar('nissan', 'leaf', 2024)
# print(my_leaf.get_descriptive_name())
# my_leaf.battery.describe_battery()
# my_leaf.battery.get_range()
